package com.ckerchina.admin.ckadmin.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class DateUtil {
	private static DateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static DateFormat dd = new SimpleDateFormat("yyyyMMddHHmmss");
	
	public static final int SECONDS_IN_DAY = 60 * 60 * 24;
    public static final long MILLIS_IN_DAY = 1000L * SECONDS_IN_DAY;
 
    public static boolean isSameDayOfMillis(final long ms1, final long ms2) {
        final long interval = ms1 - ms2;
        return interval < MILLIS_IN_DAY
                && interval > -1L * MILLIS_IN_DAY
                && toDay(ms1) == toDay(ms2);
    }
 
    private static long toDay(long millis) {
        return (millis + TimeZone.getDefault().getOffset(millis)) / MILLIS_IN_DAY;
    }
	
	public static String getTimeNow(){
		return dd.format(new Date());
	}
	
	public static String formatDateTime(Date date){
		if(date == null){
			return null;
		}
		return d.format(date);
	}
	
	public static String getNow(){
		return d.format(new Date());
	}
	
	public static Date getNowBegin(){
		String today = d.format(new Date());
		try {
			return d.parse(today.substring(0,11)+"00:00:00");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date getNowEnd(){
		String today = d.format(new Date());
		try {
			return d.parse(today.substring(0,11)+"23:59:59");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
}
